package com.meiyu.live.util;

import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;

import com.meiyu.live.R;

/**
 * Created by zbl on 2016/12/12.
 */
public class AnimationUtil {

    private static final long ANIMATION_DURATION = 500;

    /**
     * 启动透明度渐变动画
     *
     * @param visible 填入View.VISIBLE 或者View.GONE
     */
    public static void startAlphaAnimation(final View view, int visible) {
        if (visible == View.VISIBLE) {
            AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1f);
            alphaAnimation.setDuration(ANIMATION_DURATION);
            alphaAnimation.setFillAfter(true);
            alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    view.setVisibility(View.VISIBLE);
                }

                @Override
                public void onAnimationEnd(Animation animation) {

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            view.startAnimation(alphaAnimation);
        } else {
            AlphaAnimation alphaAnimation = new AlphaAnimation(1f, 0f);
            alphaAnimation.setDuration(ANIMATION_DURATION);
            alphaAnimation.setFillAfter(true);
            alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    view.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            view.startAnimation(alphaAnimation);
        }
    }

    private static final int[][] giftResMap = {
            {28, R.drawable.frame_anim_carrot, 4000},
            {34, R.drawable.frame_anim_rabbitbow, 4000},
            {36, R.drawable.frame_anim_jupiter, 4500},
            {37, R.drawable.frame_anim_dabai, 4500},
            {38, R.drawable.frame_anim_liuliuliu, 4500},
            {39, R.drawable.frame_anim_diamond, 5000},
            {40, R.drawable.frame_anim_car, 5000},
            {41, R.drawable.frame_anim_castle, 6000}
    };

    public static int[] getAnimationResByGiftId(String gift_type) {
        int[] res = {-1, -1, -1};
        try {
            int giftId = Integer.parseInt(gift_type);
            for (int[] gift : giftResMap) {
                if (gift[0] == giftId) {
                    res = gift;
                    break;
                }
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } finally {
            return res;
        }
    }
}
